﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace Service
{

    static class Program
    {
        public static string ServiceName = "Mimoza Server Service";

        static ServiceMimoza m_service = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ru-RU");
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU");

            ServiceBase[] ServicesToRun;
            m_service = new ServiceMimoza();
            ServicesToRun = new ServiceBase[] 
			{ 
				m_service 
			};

            if (Environment.GetCommandLineArgs().Contains<string>("-console"))
            {
                RunInConsoleMode();            
            }
            else
            {
                ServiceBase.Run(ServicesToRun);
            }
        }

        static void RunInConsoleMode()
        {    
            AllocConsole();

            Console.WriteLine("Running service in console mode, press a key to stop...");

            m_service.Run(Environment.GetCommandLineArgs());

            Console.ReadKey();
        
            Console.WriteLine("\nStopping...");

            m_service.Finish();
            
            Console.WriteLine("Service stopped.");
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), System.Security.SuppressUnmanagedCodeSecurity]
        [   DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AllocConsole();
    }

     
}
